package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.appuser.entity.TbUserAlipay;
import org.jeecg.modules.appuser.entity.TbUserWallet;
import org.jeecg.modules.appuser.entity.TbUserWalletDetail;
import org.jeecg.modules.appuser.mapper.TbUserWalletMapper;
import org.jeecg.modules.appuser.service.ITbUserAlipayService;
import org.jeecg.modules.appuser.service.ITbUserWalletDetailService;
import org.jeecg.modules.appuser.service.ITbUserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: tb_user_wallet
 * @Author: jeecg-boot
 * @Date: 2020-05-18
 * @Version: V1.0
 */
@Service
public class TbUserWalletServiceImpl extends ServiceImpl<TbUserWalletMapper, TbUserWallet> implements ITbUserWalletService {

    @Autowired
    @Lazy(true)
    private ITbUserWalletDetailService tbUserWalletDetailService;

    @Autowired
    private ITbUserAlipayService tbUserAlipayService;

    @Override
    public Result<?> getUserWalletDetail(String user_id) {

        Map<String, Object> resultMap = new HashMap<>();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        TbUserWallet tbUserWallet = this.baseMapper.selectOne(queryWrapper);
        if (null != tbUserWallet) {
            QueryWrapper walletqueryWrapper = new QueryWrapper();
            walletqueryWrapper.eq("wallet_id", tbUserWallet.getId());
            List<TbUserWalletDetail> tbUserWalletDetailList = tbUserWalletDetailService.list(walletqueryWrapper);
            resultMap.put("tbUserWalletDetailList", tbUserWalletDetailList);
            resultMap.put("tbUserWallet", tbUserWallet);
            TbUserAlipay tbUserAlipay = tbUserAlipayService.getOne(queryWrapper);
            resultMap.put("tbUserAlipay", tbUserAlipay);
            return Result.ok(resultMap);
        }


        return Result.ok(resultMap);
    }

    @Override
    public TbUserWallet getOneByUser_id(String user_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        return  this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<TbUserWallet> getTbUserWalletDatas(Page<TbUserWallet> page, TbUserWallet tbUserWallet) {

        return page.setRecords(this.baseMapper.getTbUserWalletDatas(page, tbUserWallet));
    }
}
